﻿<Window x:Name="window" x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:c="clr-namespace:InkType"
        xmlns:u="clr-namespace:InkType.Util;assembly=InkType.Util"
        xmlns:vm="clr-namespace:InkType.ViewModel;assembly=InkType.ViewModel"
        xmlns:acc="clr-namespace:System.Windows.Controls;assembly=Accordion"
        Title="MainWindow" Width="1440" Height="810"
        Background="#FAF8F0" Foreground="Black" FontSize="13.75"
        Closed="window_Closed">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Themes/Icons.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <FrameworkElement.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Close" CanExecute="CommandAlwaysCanExecute" Executed="CloseCommand_Executed"/>
    </FrameworkElement.CommandBindings>
    <Window.DataContext>
        <vm:InkTypeViewModel/>
    </Window.DataContext>
    <c:FlipControl x:Name="flipper" IsFlipped="False">
        <Control.CommandBindings>
            <CommandBinding Command="{x:Static c:FlipControl.FlipBackCommand}" CanExecute="CommandAlwaysCanExecute" Executed="FlipBackCommand_Executed"/>
            <CommandBinding Command="{x:Static c:FlipControl.FlipCommand}" CanExecute="CommandAlwaysCanExecute" Executed="FlipCommand_Executed"/>
        </Control.CommandBindings>
        <c:FlipControl.FlipContent>
            <Grid TextBlock.FontSize="28" TextBlock.FontWeight="Light">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="300"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <StackPanel Background="#202020">
                    <Button Command="{x:Static c:FlipControl.FlipBackCommand}">
                        <Path Data="{StaticResource iconCloseMenu}" Margin="12" StrokeThickness="2" Stroke="White"/>
                    </Button>
                    <ListBox x:Name="typefaceList" ItemsSource="{Binding Typefaces}" Foreground="White" SelectedItem="{Binding SelectedTypeface, Mode=TwoWay}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Name.Name}"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ListBox>
                    <Button Command="{Binding AddNewTypefaceCommand}">
                        <Path Data="{StaticResource iconAdd}" StrokeThickness="2" Margin="6" VerticalAlignment="Center" Stroke="White"/>
                    </Button>
                </StackPanel>
                <StackPanel Grid.Column="1" Grid.IsSharedSizeScope="True" DataContext="{Binding ElementName=typefaceList, Path=SelectedItem}" VerticalAlignment="Center">
                    <c:LabelledControl Label="Typeface Name">
                        <Border BorderThickness="1 0 0 1" BorderBrush="Black">
                        <TextBox>
                            <TextBox.Text>
                                <Binding Path="Name" Mode="TwoWay" Converter="{x:Static vm:FontName.StringConverter}" UpdateSourceTrigger="PropertyChanged">
                                    <Binding.ValidationRules>
                                        <vm:FontNameValidationRule/>
                                    </Binding.ValidationRules>
                                </Binding>
                            </TextBox.Text>
                            </TextBox>
                        </Border>
                    </c:LabelledControl>
                    <Button Content=" → Generate OTF" Padding="18 1" Command="{Binding GenerateOtfCommand}" CommandParameter="{Binding ElementName=window}"/>
                </StackPanel>
            </Grid>
        </c:FlipControl.FlipContent>
        <Grid DataContext="{Binding SelectedTypeface}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="4*"/>
                <ColumnDefinition Width="5*"/>
                <ColumnDefinition Width="2*"/>
            </Grid.ColumnDefinitions>
            <DockPanel>
                <Button DockPanel.Dock="Top" FontSize="28" FontWeight="Light" Padding="6 3" HorizontalContentAlignment="Left"
                        Content="{Binding Name.Name, FallbackValue='(No Typeface Selected)'}" Command="{x:Static c:FlipControl.FlipCommand}"/>
                <ComboBox x:Name="blockListBox" DockPanel.Dock="Top" DataContext="{Binding GlyphList}" ItemsSource="{Binding Blocks}"
                          SelectedItem="{Binding SelectedItem, Mode=TwoWay}" MaxDropDownHeight="600"
                          TextSearch.TextPath="Model.Name" VirtualizingPanel.CacheLengthUnit="Pixel">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid DataContext="{Binding Model}">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="{Binding Name}"/>
                                <TextBlock Grid.Column="1" Margin="6 0" Foreground="#808080">
                                    <Run Text="{Binding CharacterCount, Mode=OneWay}"/>
                                    <Run Text="Characters"/>
                                </TextBlock>
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                    <ItemsControl.ItemContainerStyle>
                        <Style TargetType="{x:Type ListBoxItem}">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                        </Style>
                    </ItemsControl.ItemContainerStyle>
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <VirtualizingStackPanel/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ComboBox>
                <DockPanel DataContext="{Binding ElementName=blockListBox, Path=SelectedItem}">
                    <DockPanel.Resources>
                        <CollectionViewSource x:Key="glyphsView" Source="{Binding Glyphs}"/>
                    </DockPanel.Resources>
                    <Grid DockPanel.Dock="Bottom">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <c:GlyphFilterTextBox Collection="{Binding Source={StaticResource glyphsView}}"/>
                        <StackPanel Grid.Column="1" Orientation="Horizontal">
                            <Button x:Name="shuffleButton" Content="Shuffle" Padding="18,1" VerticalAlignment="Center" Command="{Binding ShuffleCommand}"/>
                            <Button x:Name="unshuffleButton" Content="Sort" Padding="18,1" VerticalAlignment="Center" Command="{Binding UnshuffleCommand}"/>
                        </StackPanel>
                    </Grid>
                    <ListBox x:Name="glyphListBox" ItemsSource="{Binding Source={StaticResource glyphsView}}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" TextSearch.TextPath="CharCodeHex">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <VirtualizingWrapPanel ItemHeight="60" ItemWidth="48"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <TextBlock x:Name="glyph" FontSize="32" Visibility="Collapsed" Text="{Binding Char}" Opacity="0.15"
                                                FontFamily="{Binding ElementName=fontFamilyListBox, Path=SelectedItem}"
                                                HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                    <Viewbox>
                                        <c:StrokeThumbnail x:Name="thumbnail" Height="{x:Static vm:GlyphMetrics.Height}" Width="{Binding GlyphWidth}"
                                                            Strokes="{Binding StrokeCollection}"/>
                                    </Viewbox>
                                    <TextBlock Text="{Binding CharCodeHex}" Grid.Row="1" TextAlignment="Center"/>
                                </Grid>
                                <DataTemplate.Triggers>
                                    <DataTrigger Binding="{Binding StrokeCollection.Count}" Value="0">
                                        <Setter TargetName="glyph" Property="Visibility" Value="Visible"/>
                                        <Setter TargetName="thumbnail" Property="Visibility" Value="Collapsed"/>
                                    </DataTrigger>
                                </DataTemplate.Triggers>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                        <ItemsControl.ItemContainerStyle>
                            <Style TargetType="ListBoxItem">
                                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                                <Setter Property="Padding" Value="0"/>
                            </Style>
                        </ItemsControl.ItemContainerStyle>
                    </ListBox>
                </DockPanel>
            </DockPanel>
            <c:GlyphEditingArea x:Name="editingArea" Grid.Column="1" DataContext="{Binding ElementName=glyphListBox, Path=SelectedItem}" Zoom="1" 
                                DrawingAttributes="{Binding ElementName=tipTuner, Path=DrawingAttributes}"/>
            <acc:Accordion Grid.Column="2" DataContext="{Binding ElementName=glyphListBox, Path=SelectedItem}">
                <acc:AccordionItem Header="Glyph Info">
                    <StackPanel Grid.IsSharedSizeScope="True">
                        <Border Background="#1000">
                            <Border HorizontalAlignment="Center" Background="#1000" Margin="6">
                                <Viewbox Height="168">
                                    <TextBlock Text="{Binding Char, FallbackValue='null'}"
                                        FontFamily="{Binding ElementName=fontFamilyListBox, Path=SelectedItem}"/>
                                </Viewbox>
                            </Border>
                        </Border>
                        <c:LabelledControl Label="Character:">
                            <TextBox IsReadOnly="True" Text="{Binding Char, Mode=OneWay}"/>
                        </c:LabelledControl>
                        <c:LabelledControl Label="Unicode:">
                            <TextBox IsReadOnly="True" Text="{Binding CharCode, Mode=OneWay}"/>
                        </c:LabelledControl>
                        <c:LabelledControl Label="Unicode (Hex):">
                            <TextBox IsReadOnly="True" Text="{Binding CharCode, StringFormat='0x{0:X4}', Mode=OneWay}"/>
                        </c:LabelledControl>
                        <c:LabelledControl Label="Advance Mode:">
                            <StackPanel>
                                <ComboBox ItemsSource="{u:EnumValues {x:Type vm:GlyphAdvanceMode}}" SelectedItem="{Binding AdvanceMode, Mode=TwoWay}"/>
                                <StackPanel Visibility="{Binding AdvanceMode, Converter={x:Static vm:GlyphAdvanceModes.NeedParamConverter}}">
                                    <Slider Minimum="0" Maximum="1000" SmallChange="1" LargeChange="50" Value="{Binding AdvanceModeParam, Mode=TwoWay}"/>
                                    <TextBlock Grid.Column="2" Text="{Binding AdvanceModeParam, Mode=OneWay}" Margin="6 0"/>
                                </StackPanel>
                            </StackPanel>
                        </c:LabelledControl>
                        <c:LabelledControl Label="Glyph Width:">
                            <TextBox IsReadOnly="True" Text="{Binding GlyphWidth, Mode=OneWay}"/>
                        </c:LabelledControl>
                        <c:StylusTipTuner x:Name="tipTuner" Height="200" Visibility="Collapsed"/>
                    </StackPanel>
                </acc:AccordionItem>
                <acc:AccordionItem Header="Placeholder Font">
                    <ListBox x:Name="fontFamilyListBox" Grid.Row="1" ItemsSource="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
                        <FrameworkElement.DataContext>
                            <CollectionViewSource Source="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
                                <CollectionViewSource.SortDescriptions>
                                    <scm:SortDescription xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
                                                    PropertyName="Source" Direction="Ascending"/>
                                </CollectionViewSource.SortDescriptions>
                            </CollectionViewSource>
                        </FrameworkElement.DataContext>
                        <ListBox.SelectedItem>
                            <FontFamily>Microsoft Yahei</FontFamily>
                        </ListBox.SelectedItem>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <TextBlock Text="{Binding}" FontSize="9"/>
                                    <TextBlock Text="{Binding}" FontSize="13.7" FontFamily="{Binding}" Margin="12 0 0 0" TextOptions.TextHintingMode="Animated"/>
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ListBox>
                </acc:AccordionItem>
            </acc:Accordion>
        </Grid>
    </c:FlipControl>
</Window>
